package dao;

import java.io.*;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String driver = null;
    private static String url = null;
    private static String user = null;
    private static String password = null;

    static {
        init();
    }

    private static void init() {
        Properties properties = new Properties();
        InputStream is = null;
        try {
            //根据字符流来读取文字
            is = new FileInputStream(new File("src/main/resources/database.properties"));
            properties.load(is);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取理解对象
     *
     * @return
     */
    public Connection getConnection() {
        try {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, user, password);
            return connection;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭所有连接
     *
     * @param rs   返回所有的结果集
     * @param stmt sql执行对象 执行sql语句
     * @param conn 连接对象
     */
    private void closeAll(ResultSet rs, Statement stmt, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }

            if (stmt != null) {
                stmt.close();
            }

            if (conn != null) {
                conn.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 执行增删改的方法
     *
     * @param sql   需要执行的sql语句
     * @param param sql带参数
     * @return 返回受影响行数
     */
    public int executeUpdate(String sql, Object[] param) {
        PreparedStatement stmt = null;
        int count = 0;
        Connection conn = null;
        try {
            //通过数据连接对象获取sql执行对象，并且告诉你需要执行哪个SQL语句
            conn = getConnection();
            stmt = conn.prepareStatement(sql);
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    stmt.setObject((i + 1), param[i]);
                }
            }
            count = stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(null, stmt, conn);
        }
        return count;
    }

    /**
     * 执行查询的语句
     *
     * @param sql   用户需要的sql
     * @param param 参数
     * @return 结果集
     */
    public ResultSet excuteQuery(String sql, Object[] param) {
        PreparedStatement stmt = null;
        Connection connection = null;
        ResultSet rs = null;
        try {
            connection = getConnection();
            stmt = connection.prepareStatement(sql);
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    stmt.setObject((i + 1), param[i]);
                }
            }
            rs = stmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
}
